import Vue from 'vue';
import Router from 'vue-router';
import { getPositionY, smoothScrollTop, topWindScrollY, } from "@/assets/js/utils/utils.js";

Vue.use(Router);

/**
 * 重写路由的push方法
 */
const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return routerPush.call(this, location).catch(error => error)
}

let router = new Router({
	routes: [{
			path: '/',
			redirect: '/home', //重定向
		},
		//首页
		{
			path: '/home',
			name: 'home',
			component: resolve => require(['@/pages/home/home'], resolve),
			meta: {
				title: '首页',
				showNav: true, //控制底部导航是否显示
				keepAlive: true,
				scrollTop: 0,
			},
		},
		//分类
		{
			path: '/classify',
			name: 'classify',
			component: resolve => require(['@/pages/classify/classify'], resolve),
			meta: {
				title: '分类',
				showNav: true,
				keepAlive: true,
				scrollTop: 0,
			},
		},
		//购物车
		{
			path: '/cart',
			name: 'cart',
			component: resolve => require(['@/pages/cart/cart'], resolve),
			meta: {
				title: '购物车',
				showNav: true,
				keepAlive: true,
				scrollTop: 0,
			},
		},
		//我的
		{
			path: '/mine',
			name: 'mine',
			component: resolve => require(['@/pages/mine/mine'], resolve),
			meta: {
				title: '我的',
				showNav: true,
				keepAlive: false,
				scrollTop: 0,
			},
		},
		//用户中心
		{
			path: '/user',
			name: 'user',
			component: resolve => require(['@/pages/mine/user/user'], resolve),
			meta: {
				title: '用户中心',
				scrollTop: 0,
			},
		},
		//消息列表
		{
			path: '/message',
			name: 'message',
			component: resolve => require(['@/pages/mine/message/message'], resolve),
			meta: {
				title: '消息',
				scrollTop: 0,
			},
		},
		//设置
		{
			path: '/setting',
			name: 'setting',
			component: resolve => require(['@/pages/mine/setting/setting'], resolve),
			meta: {
				title: '设置',
				scrollTop: 0,
			},
		},
		//订单
		{
			path: '/order',
			name: 'order',
			component: resolve => require(['@/pages/order/order'], resolve),
			meta: {
				title: '订单',
				scrollTop: 0,
			},
		},
		//收藏
		{
			path: '/collect',
			name: 'collect',
			component: resolve => require(['@/pages/mine/collect/collect'], resolve),
			meta: {
				title: '收藏',
				scrollTop: 0,
			},
		},
		//咨询
		{
			path: '/consult',
			name: 'consult',
			component: resolve => require(['@/pages/mine/consult/consult'], resolve),
			meta: {
				title: '咨询',
				scrollTop: 0,
			},
		},
		//历史
		{
			path: '/history',
			name: 'history',
			component: resolve => require(['@/pages/mine/history/history'], resolve),
			meta: {
				title: '历史',
				scrollTop: 0,
			},
		},
		//地址
		{
			path: '/addr',
			name: 'addr',
			component: resolve => require(['@/pages/mine/addr/addr'], resolve),
			meta: {
				title: '地址',
				scrollTop: 0,
			},
		},
		//核对订单
		{
			path: '/orderVerify',
			name: 'orderVerify',
			component: resolve => require(['@/pages/orderVerify/orderVerify'], resolve),
			meta: {
				title: '核对订单',
				scrollTop: 0,
			},
		},
		//支付成功
		{
			path: '/paySuccess',
			name: 'paySuccess',
			component: resolve => require(['@/pages/payState/paySuccess'], resolve),
			meta: {
				title: '支付成功',
				scrollTop: 0,
			},
		},
		//支付失败
		{
			path: '/payFailure',
			name: 'payFailure',
			component: resolve => require(['@/pages/payState/payFailure'], resolve),
			meta: {
				title: '支付失败',
				scrollTop: 0,
			},
		},
		//商品详情
		{
			path: '/goodsDetail',
			name: 'goodsDetail',
			component: resolve => require(['@/pages/goodsDetail/goodsDetail'], resolve),
			meta: {
				title: '商品详情',
				scrollTop: 0,
			},
		},

		//404
		{
			path: '/404',
			name: '404',
			component: resolve => require(['@/pages/404/404'], resolve),
			meta: {
				title: '404',
			}
		},
		{
			path: '*',
			redirect: '/404'
		}
	]
});

router.beforeEach((to, from, next) => {
	document.title = to.meta.title ? to.meta.title : 'vue-app';
	next();
});

let routerList = ['mine', 'cart', 'orderVerify', 'goodsDetail']; //需要置顶的路由
router.afterEach((to, from) => {
	let scrollTop = document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset;
	from.meta.scrollTop = scrollTop;
	//console.log(from)
	for(let r of routerList) {
		if(to.name == r) {
			//topWindScrollY(0);
			smoothScrollTop();
		}
	}
});

export default router;